package com.wlanauth.web.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Restrictions;

import com.wlanauth.core.criteria.GeneralCriteria;
import com.wlanauth.core.criteria.GeneralCriteriaResult;
import com.wlanauth.domain.manage.Role;
import com.wlanauth.domain.manage.SystemResource;
import com.wlanauth.service.manage.RoleService;
import com.wlanauth.web.core.BaseActionSupport;
import com.wlanauth.web.core.JsonResult;
import com.wlanauth.web.core.JsonResultConfig;
/**
 * 角色
 * @author zz
 * @date 2012-5-23
 * @email charles.zz.life@gmail.com
 */
public class RoleAction extends BaseActionSupport {

	private static final long serialVersionUID = 1L;
	
	private List<SystemResource> systemResources = new ArrayList<SystemResource>();
	/**
	 * 实体
	 */
	private Role entity;
	/**
	 * 角色服务接口
	 */
	private RoleService roleService;
	
	public String list(){
		GeneralCriteria criteria = this.buildWebQueryCriteria(Role.class);
		if(entity!=null){
			if(entity.getCode()!=null){
				criteria.add(Restrictions.eq("code", entity.getCode()));
			}
			
		}
		//取当前管理员以下角色
		criteria.add(Restrictions.le("level", this.getCurrentOperator().getRole().getLevel()));
		
		GeneralCriteriaResult<Role> result = criteria.executeQuery();
		this.jsonResult = new JsonResult(this,result.getData(),result.getTotal());
		JsonResultConfig cfg = new JsonResultConfig();
		cfg.setExcludeProperties(new String[]{
				".*roles",
				".*systemResources"
		});
		return this.actionJsonResult(cfg);
	}
	
	public String getAllRoles(){
		
		GeneralCriteria criteria = new GeneralCriteria(Role.class);
		//取当前管理员以下角色
		criteria.add(Restrictions.le("level", this.getCurrentOperator().getRole().getLevel()));
		GeneralCriteriaResult<Role> result = criteria.executeQuery();
		JsonResultConfig cfg = new JsonResultConfig();
		cfg.setExcludeProperties(new String[]{
				".*systemResources"
		});
		this.jsonResult = new JsonResult(this,result.getData(),true);
		return this.actionJsonResult(cfg);
	}
	/**
	 * 保存
	 * @return
	 */
	public String save(){
		Role temp = this.roleService.getByCode(entity.getCode());
		if(temp != null){
			this.jsonResult = new JsonResult(this,"该编码已存在,CODE:"+entity.getCode(),false);
			return this.actionJsonResult();
		}
		Set<SystemResource> resources = null;
		if(this.systemResources!=null&&this.systemResources.size()>0){
			resources = new HashSet<SystemResource>();
			for(SystemResource r:this.getSystemResources()){
				if(r!=null&&StringUtils.isNotBlank(r.getId())){
					resources.add(r);
				}
			}
		}
		this.entity.setEditable(true);
		
		Date now = new Date();
		this.entity.setCreateTime(now);
		this.entity.setUpdateTime(now);
		this.entity.setSystemResources(resources);
		this.entity.setLevel(Role.LEVEL_NOMAL);
		this.entity.setCode("CODE_"+new SimpleDateFormat("yyyyMMddHHmmss").format(now));
		this.roleService.save(this.entity);
		this.jsonResult = new JsonResult(this,this.entity.getId(),true);
		return this.actionJsonResult();
	}
	/**
	 * 修改
	 * @return
	 */
	public String edit(){
		Role r = this.roleService.get(entity.getId());
		this.jsonResult = new JsonResult(this,r,true);
		JsonResultConfig cfg = new JsonResultConfig();
		cfg.setExcludeProperties(new String[]{
				".*roles"
		});
		return this.actionJsonResult(cfg);
	}
	/**
	 * 更新
	 * @return
	 */
	public String update(){
		Role r = this.roleService.get(entity.getId());
		if(r == null){
			this.jsonResult = new JsonResult(this,"该角色不存在",false);
			return this.actionJsonResult();
		}
		if(!r.getEditable()){
			this.jsonResult = new JsonResult(this,"该角色不可编辑",false);
			return this.actionJsonResult();
		}
		if(!r.getCode().equals(entity.getCode())){//换编码需较验
			Role temp = this.roleService.getByCode(entity.getCode());
			if(temp!=null){
				this.jsonResult = new JsonResult(this,"该编码已存在,CODE:"+entity.getCode(),false);
				return this.actionJsonResult();
			}
		}
		
		Set<SystemResource> resources = null;
		if(this.systemResources!=null&&this.systemResources.size()>0){
			resources = new HashSet<SystemResource>();
			for(SystemResource sr:this.getSystemResources()){
				if(sr!=null&&StringUtils.isNotBlank(sr.getId())){
					resources.add(sr);
				}
			}
		}
		r.setSystemResources(resources);
		
		r.setUpdateTime(new Date());
		r.setCode(this.entity.getCode());
		r.setDescription(this.entity.getDescription());
		r.setName(this.entity.getName());
		this.roleService.update(r);
		this.jsonResult = new JsonResult(this,this.entity.getId(),true);
		return this.actionJsonResult();
	}
	/**
	 * 删除
	 * @return
	 */
	public String delete(){
		this.roleService.deleteByKey(this.entity.getId());
		this.jsonResult = new JsonResult(this,true);
		return this.actionJsonResult();
	}
	public void setSystemResources(List<SystemResource> systemResources) {
		this.systemResources = systemResources;
	}

	public List<SystemResource> getSystemResources() {
		return systemResources;
	}

	public void setEntity(Role entity) {
		this.entity = entity;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public Role getEntity() {
		return entity;
	}
	
	
	
	
}
